/**
 *轻云蒜瓣应用商城面板
 */
  Ext.define("PRO.shop.view.usershop.shopMarketView",{
  	extend: 'Ext.panel.Panel',
    border:0,
    layout:'fit',
    alias: 'widget.shop.shopMarketView',
    modal : true,
 	initComponent: function(){
         var me = this;
     	var store=Ext.create('Ext.data.Store', {
                    fields : ['src','code','name','productMs','pjnum','sellnum','service','phone','money'],
                    data :me.doLoadData()
                });
        var imageTpl = new Ext.XTemplate(
            '<div class="marketCenter">',
            '<div class="marketCenterCon">',
            '<tpl for=".">',
                '<div class="marketCenterItem">',
                    '<div class="marketCenterItemImg"><a class="goDetail" data-code={code} data-money={money}><img src="{src}"/></a></div>',
                    '<div  class="marketItemText">',
                        '<a href="javascript:;"><p class="marketItemName goDetail" data-code={code} data-money={money}>{name}</p></a>',
                        '<div class="marketItemCon goDetail" data-code={code} data-money={money}>{productMs}</div>',
                    '</div>',
                    '<div class="marketItemTextRight">',
                       '<tpl if="money &gt; 0">',
                            '<p class="marketItemMoney">¥{money}</p>',
                            '<a href="javascript:;"><p class="shopBtn" data-name={name} data-code={code}>立即购买</p></a>',
                        '<tpl else>',
                            '<a href="javascript:;"><p class="forFree">永久免费</p></a>',
                        '</tpl>',
                    '</div>',
                '</div>',
            '</tpl>',
            '</div>',
            '</div>'
        );

        me.items=[{
            xtype:'dataview',
            tpl:imageTpl,
            store:store,
            itemId:'view',
            style:'height:100%;',
            listeners:{//refresh
                 afterrender:function(view,e){
                     view.getEl().on('click',function(e){
                        if(e.getTarget('.goDetail',2)){
                            var goDetail = e.getTarget('.goDetail',2);
                            var productId = goDetail.getAttribute('data-code');
                            var money = goDetail.getAttribute('data-money');
                            var isbtnShow = me.isBtnShow(money);
                            JE.win({
                                title:'产品详情',
                                width:1200,//JE.getBodyWidth()-750,
                                items:[{xtype:'shop.productIntro',productId:productId,_this:me.up('[xtype=shop.shopview]'),isbtnShow:isbtnShow}]
                            }).show();
                        }
                        if(e.getTarget('.shopBtn',1)){
                            //此处获取唯一的id；
                            var shopBtn = e.getTarget('.shopBtn',1);
                            var name = shopBtn.getAttribute('data-name');
                            var productId = shopBtn.getAttribute('data-code');
                            JE.ajax({
                                url:'/je/shoppingmall/bill/getBillSnapshot',
                                params:{
                                    productId:productId 
                                },
                                success:function(res){
                                    var res = JSON.parse(res.responseText);
                                    if(res.success){
                                        var winName = res.obj.productInfo.values.CP_CPTYPE_CODE;
                                        var name = res.obj.productInfo.values.CP_MC;
                                        JE.win({
                                            xtype:'panel',
                                            title:'订单确认',
                                            layout:'fit',
                                            width:840,//JE.getBodyWidth()-750,
                                            height:(JE.getBodyHeight()-300)>=563?JE.getBodyHeight()-300:563,//563
                                            border:0,
                                            draggable : false,
                                            bodyPadding:'20px 40px',
                                            default:{border:0},
                                            items:[{xtype:'shop.shoppayview',winName:winName=='MENUP'?false:true,resData:res,name:name,type:'save'}]
                                        }).show();
                                    }else{
                                        JE.alert('后台出错，请联系开发人员！')
                                    }
                                }
                            })
                            
                        }
                     });
                 }
             },
        }];
     	me.callParent(arguments);
     },
     doLoadData:function(){
        var me = this;
        var datas=[];
        var redata = JE.ajax({
            url:'/je/shoppingmall/product/loadProducts',
            params:{}
        });
        if(redata.success){
            var resObj = redata.obj;
            Ext.each(resObj,function(itemData){
                datas.push({
                    src :JE.getFileUrl(itemData.CP_TB.split('*')[1],'PROJECT','','','JE_SAAS_CP'),//'/je/doc/document/doLoadFile?jeFileType=PROJECT&path='+itemData.CP_TB.split('*')[1],//JE.buildCdnUrl  ,//商品图片
                    code:itemData.JE_SAAS_CP_ID,//商品数据ID
                    name:itemData.CP_MC,//商品名称
                    pjnum:itemData.pjsl,//商品评价数
                    sellnum:itemData.xssl,//商品购买量
                    service:'',//客服
                    phone:'400-0999235',//电话
                    money:me.setNumStyle(itemData.CP_MONEY),//价格
                    productMs:itemData.productDesc
               })
            })
        }
        return datas;
     },
     isBtnShow:function(money){
         if(money &&money>0){
            return false;
         }else{
            return true;
         }
     },
     setNumStyle:function(num){
        num = num.toString();
         if(num.includes('.')){
            if(num.substr(num.indexOf('.')+1).length==2){
                return num
            }else{
            return num = num+'0';
            }
         }else{
            return num= num+'.00';
         }
     }
 });
